"""
Reinforcement learning maze example.

Red rectangle:          explorer.
Black rectangles:       hells       [reward = -1].
Yellow bin circle:      paradise    [reward = +1].
All other states:       ground      [reward = 0].

This script is the main part which controls the update method of this example.
The RL is in RL_brain.py.

"""

from maze_env import Maze  # 环境
from RL_brain import QLearningTable  # 大脑


def update():
    for episode in range(100):
        # initial observation
        observation = env.reset()  # 从环境中得出的观测值，reset初始化

        while True:
            # 刷新一下环境
            env.render()

            # 基于观测值选择动作
            action = RL.choose_action(str(observation))

            # 根据动作得到下一个观测值和奖励。done是否碰到障碍或到达目标，对应第40代码，observation就是观测值
            observation_, reward, done = env.step(action)

            # 若回合没结束，RL就从第一个观测中学习动作和奖励已经下一个观测
            RL.learn(str(observation), action, reward, str(observation_))

            # swap observation
            observation = observation_

            # break while loop when end of this episode
            if done:
                break

    # end of game
    print("game over")
    env.destroy()


if __name__ == "__main__":
    env = Maze()
    RL = QLearningTable(actions=list(range(env.n_actions)))

    env.after(100, update)
    env.mainloop()
